package ngat.oss;

import ngat.message.OSS.*;
import ngat.util.*;
import ngat.util.logging.*;
import ngat.phase2.*; 
import ngat.instrument.*;
import ngat.phase2.nonpersist.*;

import java.io.*;
import java.lang.reflect.*;

import jyd.dbms.*;
import jyd.storable.*;
import jyd.collection.*;

/**
 * Action: Modify a Users details
 * @author S.N.Fraser
 * $Id$
 */
public class EDIT_USERImpl extends TransactionImpl {
    
    /** The User data to insert into db root.*/
    protected User user;
    
    public EDIT_USERImpl(User user) {
	super();
	this.user = user;
    }
    
    public EDIT_USERImpl(EDIT_USER request) throws InvocationTargetException {
	super(request);
	this.user = request.getUser();	
    }

    /**
     * Modfies the User in the DB.
     * @return result.
     */
    public TRANSACTION_DONE exec() { 

	Transaction t = null;
	try {
	    t = Transaction.current();
 
	    Root root = OSS_Utilities.fetchRoot();
	    
	    // Check if Root exists.
	    if (root == null) {
		logger.log(1, "EDITUSER_Impl", id, "exec",
			   "Root not found in database.");
		return error(TRANSACTION.PATH_RESOLUTION_ERROR,
				     "Could not find DB Root");
	    }

	    Path userPath = new Path(user.getFullPath());
	   
	    // the current USER
	    User  cuser     = OSS_Utilities.fetchUser(userPath);

            if (cuser == null)
                return error(TRANSACTION.PATH_RESOLUTION_ERROR,
                             "No such USER ["+userPath+"] found");

	    // copy all fields across...

	    cuser.setSname(user.getSname());
	    cuser.setFname(user.getFname());
	    cuser.setOrganization(user.getOrganization());
	    cuser.setAddress1(user.getAddress1());
	    cuser.setCountry(user.getCountry());
	    cuser.setEmail(user.getEmail());
	    cuser.setTelephone(user.getTelephone());
	    
	    EDIT_USER_DONE done = new EDIT_USER_DONE(id);
	    done.setSuccessful(true);
	    return done;

	} finally {
	    t.commit();
	}
	
    }
    
}

/** $Log$ */
